package com.dldata.devtools30.controller.api;

import com.dldata.devtools30.entity.DevtApiTypeEntity;
import com.dldata.devtools30.repository.api.DevtApiTypeEntityDao;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(value = "/DevtApiTypeController")
public class DevtApiTypeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    DevtApiTypeEntityDao devtApiTypeEntityDao;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
    
        List<DevtApiTypeEntity> pFunctiontypeEntities =new ArrayList() ;
        Iterable<DevtApiTypeEntity> typeEntityDaoAll = devtApiTypeEntityDao.findAll();
        Iterator<DevtApiTypeEntity> entityIterator = typeEntityDaoAll.iterator();
        while (entityIterator.hasNext()) {
            DevtApiTypeEntity devtApiTypeEntity = entityIterator.next();
            pFunctiontypeEntities.add(devtApiTypeEntity);
        }
        return Result.ok(pFunctiontypeEntities);
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request,
                                    HttpServletResponse response)throws Exception{
        if(StringUtil.isEmptyOrLength0(id)){
            return Result.error("参数有误");
        }
        try {
            devtApiTypeEntityDao.deleteById(id);
            return Result.ok("删除成功！");

        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("【执行"+this.getClass().getName()+".deleteKeShi() 方法失败 】异常："+ex.getMessage(),ex);
            return Result.error("系统错误，请稍候再试");
        }

    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(HttpServletRequest request,
                                    HttpServletResponse response,
                                    ModelMap model, DevtApiTypeEntity pFunctiontypeEntity) throws IOException {
        if(StringUtil.isEmptyOrLength0(pFunctiontypeEntity.getMc()) ){
            return Result.error("名称不能为空");
        }
        if(StringUtil.isEmptyOrLength0(pFunctiontypeEntity.getDm()) ){
            return Result.error("代码不能为空");
        }
        try {
            if(StringUtil.isNullOrSpace(pFunctiontypeEntity.getId())){
                pFunctiontypeEntity.setId(UUID.randomUUID().toString());
            }
            devtApiTypeEntityDao.save(pFunctiontypeEntity);
            return Result.ok("保存成功！");

        }catch (Exception ex){

            logger.error("【执行"+this.getClass().getName()+".saveKeShi() 方法失败 】异常："+ex.getMessage());
            return Result.error("系统错误，请稍候再试");
        }

    }

}
